<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="js/bootstrap-3.3.5-dist/css/bootstrap.min.css" />
		<script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/bootstrap-3.3.5-dist/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			body {
				margin: 0 auto auto auto;
			}
			
			#panel-title {
				height: 70px;
			}
			
			#textbody1 {
				display: block;
				width: 600px;
				height: 900px;
			}
			
			#homepage-button {
				float: right;
			}
			
			#texttitle {
				float: left;
			}
			input[type=radio]{
				width: 15px;
				height: 15px;
				display: inline;
			}
			input[type=file]{
				display: none;
			}
			#download{
				width: 600px;
				height: 30px;
			}
		</style>
	</head>

	<body>
		<nav class="nav nav-justified navbar-fixed-top">
			<div class="container-fluid">

				<div class="navbar-header">

					<a href="home-page.html" class="navbar-brand">首页</a>

				</div>
				<ul class="nav navbar-nav navbar-right">
					<li id="updata">
						<button type="button" class="btn btn-default">
					<a id="">提交</a>
					</button>

					</li>
				</ul>
			</div>

			<form >
				<div class="panel-body center-block" id="textbody1">
					<div class="form-group">
						<input type="text" class="form-control" placeholder="请输入标题" name="input-name"/>
						<div id="download">
							<input type="radio" class="form-control" /><span style="display: inline;">上传文件</span>
							<input type="file" name="file-name"/>
						</div>
						
						<textarea class="form-control" name="textarea-name" placeholder="想说点什么?"></textarea>
					</div>
				</div>
			</form>
		</nav>
		<script type="text/javascript">
			window.onload = function() {
				$("#updata").click(function() {
				
					if($("input[type=radio]").attr("checked")=="checked"){
					if($("input[type=file]").val()==null||$("input[type=file]").val()==''){
					  alert("请选择文件上传！")
					  return
					}
						var txt = new FormData($("form")[0]);
	
						//					console.log(txt);
						//                    var ur = 'http://127.0.0.1:8080/forum/Upservlet'
						$.ajax({
							url: "Upservlet",
							type: "post",
							dataType: "text",
							async: true,
							data: txt,
							enctype: "multipart/form-data",
							processData: false,
							contentType: false,
	
							success: function() {
								alert("提交成功了！");
								window.location.href = "发帖.jsp";
							}
						})
					}else{
						$.ajax({
							type:"post",
							url:"UpservletNoFile",
							async:true,
							data:$("form").serialize(),
							success:function(){
								alert("提交成功了！")
								window.location.href="发帖.jsp"
							}
						});
					}
				});
				
				$("input[type=radio]").click(function(){
					if($("input[type=radio]").attr("checked")=="checked"){
						$("input[type=radio]").removeAttr("checked");
						$("input[type=file]").css("display","none");
					}else{
						$("input[type=radio]").attr("checked","checked");
						$("input[type=file]").css("display","inline");
					}
				});
			}
		</script>

	</body>

</html>